/**
 * 
 */
package com.newworld.dmsapp2.fragment.adapter;

/**
 * @author zuo
 *
 */
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.fragment.bean.HouseManagePeople;
import com.newworld.dmsapp2.util.Config;

/**
 * @author zuo 历史查询列表界面的adapter
 * 
 */
public class HouseManagePeopleAdapter extends BaseAdapter {

	private ArrayList<HouseManagePeople> housepeopleList;
	private LayoutInflater inflater;
	Context context;
	private Intent intent;

	public HouseManagePeopleAdapter(Context context,
			ArrayList<HouseManagePeople> housepeopleList) {
		inflater = LayoutInflater.from(context);
		this.housepeopleList = housepeopleList;
		this.context = context;
	}

	@Override
	public int getCount() {
		return housepeopleList.size();
	}

	@Override
	public Object getItem(int position) {
		return housepeopleList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint({ "ResourceAsColor", "NewApi" })
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.activity_house_manage_people_list_item, parent,
					false);
			holder = new ViewHolder();
			holder.relationTv = (TextView) convertView
					.findViewById(R.id.tv_house_people_item_relation);
			holder.nameEdit = (EditText) convertView
					.findViewById(R.id.tv_house_people_item_name);
			holder.phonenumTv = (TextView) convertView
					.findViewById(R.id.tv_house_people_item_phonenumber);
			holder.agreeTv = (TextView) convertView
					.findViewById(R.id.tv_house_peoplep_item_iftagree);
			holder.noteTv = (TextView) convertView
					.findViewById(R.id.tv_house_people_item_note);
			holder.notelbTv = (TextView) convertView
					.findViewById(R.id.tv_hosue_people_notelb);
			holder.relationTv.setTag(position);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
			holder.relationTv.setTag(position);
		}
		// HouseManagePeople item = housepeopleList.get(position);

		holder.relationTv.setText(housepeopleList.get(position)
				.getRelationType_typeName());
		if (null != housepeopleList.get(position).getRealName()
				&& housepeopleList.get(position).getRealName() != "") {
			housepeopleList.get(position).setNameMix(
					housepeopleList.get(position).getRealName() + "（"
							+ housepeopleList.get(position).getUser_userName()
							+ "）");
		} else {
			housepeopleList.get(position).setNameMix(
					housepeopleList.get(position).getUser_userName());
		}

		holder.nameEdit.setBackground(null);
		holder.nameEdit.setText(housepeopleList.get(position)
				.getUser_userName());
		if (null != housepeopleList.get(position).getUser_mobilePhone()
				&& !housepeopleList.get(position).getUser_mobilePhone()
						.equals("")) {
			holder.phonenumTv.setText(housepeopleList.get(position)
					.getUser_mobilePhone());
			holder.phonenumTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); // 下划线
			holder.phonenumTv.getPaint().setAntiAlias(true);// 抗锯齿
		} else {
			holder.phonenumTv.setVisibility(View.GONE);
		}

		if (!housepeopleList.get(position).getStatusType_typeCode()
				.equals(Config.HOUSE_MANAGEMENT_ATATCHED)) {
			holder.agreeTv.setVisibility(View.VISIBLE);
			holder.agreeTv.setText(housepeopleList.get(position)
					.getStatusType_typeName());
		} else {
			holder.agreeTv.setVisibility(View.GONE);

		}
		if (null != housepeopleList.get(position).getPostscript()
				&& !housepeopleList.get(position).getPostscript().equals("")) {
			holder.noteTv
					.setText(housepeopleList.get(position).getPostscript());
		} else {
			holder.noteTv.setVisibility(View.INVISIBLE);
			holder.notelbTv.setVisibility(View.INVISIBLE);
		}

		holder.phonenumTv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				int position = (Integer) holder.relationTv.getTag();
				intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
						+ housepeopleList.get(position).getUser_mobilePhone()));
				context.startActivity(intent);
			}

		});

	

		if (null != housepeopleList && position < housepeopleList.size()) {
			if ("" != housepeopleList.get(position).getRealName()) {
				holder.nameEdit.setText(housepeopleList.get(position)
						.getNameMix().toString());
			} else {
				holder.nameEdit.setText(housepeopleList.get(position)
						.getUser_userName());
			}
		}

		return convertView;
	}

	
	@Override
	public int getItemViewType(int position) {
		// current menu type
		int ifagreenum = 0;
		if (!housepeopleList.get(position).getStatusType_typeCode()
				.equals(Config.HOUSE_MANAGEMENT_ATATCHED)) {
			ifagreenum = 0;
		} else if (housepeopleList.get(position).getStatusType_typeCode()
				.equals(Config.HOUSE_MANAGEMENT_ATATCHED)) {
			ifagreenum = 1;
		}
		return ifagreenum;
	}

	private class ViewHolder {

		TextView relationTv;
		EditText nameEdit;
		TextView phonenumTv;
		TextView agreeTv;
		TextView noteTv;
		TextView notelbTv;

	}
}